package eScrum.pages;

import org.apache.wicket.Application;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.spring.injection.annot.SpringBean;

import eScrum.entities.User;
import eScrum.entities.UserStatus;
import eScrum.services.UserService;

public class ValidationPage  extends WebPage {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3076923662524904429L;

	@SpringBean
	private UserService userService;

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public ValidationPage(PageParameters params) {

		User u;
		System.out.println(params.get("user") + " " + params.get("token"));
		u = userService.findUserByUserName(params.get("user").toString());
		if (u != null) {
			System.out.println(u.getStatus());
			if (u.getStatus() != UserStatus.ACTIVE) {
				u.setStatus(UserStatus.ACTIVE);
				userService.saveOrUpdate(u);
				System.out.println("Active!");
				add(new Label("response",
						"Activated! Redirecting in 5 seconds."));
				add(new RedirectBehaviour(Application.get().getHomePage(), 5));
			} else {
				System.out.println("Is already active!");
			}
		} else {
			System.out.println("User does not exist");
			add(new Label("response",
					"User does not exist! Redirecting to homepage."));
			add(new RedirectBehaviour(Application.get().getHomePage(), 5));
		}
	}
}
